<?php
$csvfile = "top600companies.csv";
$outputfile = "top600companylist.sql";
$startindex = 1;

/***********************************************/
function processRow($counter, $rowdata, $out) {
    $shortName = $rowdata[0];
    $address   = $rowdata[1];
    $comtact1  = $rowdata[2];
    $contact2  = $rowdata[3];
    $phone     = explode(",", $rowdata[4]);
    $fax       = explode(",", $rowdata[5]);
    $email1    = $rowdata[6];
    $email2    = $rowdata[7];
    $hp        = $rowdata[9];
    if ($shortName == "") { return; }
    $insert1 = "INSERT INTO extracompanies (companyname, address, email1, email2, phone1, phone2, fax1, fax2, contactperson1, contactperson2, homepage, companytype) ".
              "VALUES ('$shortName', '$address', '$email1', '$email2', '$phone[0]', '$phone[1]', '$fax[0]', '$fax[1]', '$contact1', '$contact2', '$hp', 1);\r\n";
    fputs($out, $insert1);
}
/***********************************************/

$f = fopen($csvfile, "r");
$c = 0;
$out = fopen($outputfile,  "w");
$counter = $startindex;
while (!feof($f)) {
    $c++;
    $row = rtrim(fgets($f));
    if ($c == 1) { continue; }
    $row = str_replace("\"", "", $row);
    $row = str_replace("'", "''", $row);
    $rowdata = explode(";", $row);
    processRow($counter, $rowdata, $out);
    $counter++;
}
fclose($f);
fclose($out);
?>
